//
//  HSMessageViewModel.swift
//  HomeSchoolDemo
//
//  Created by rimi on 16/10/11.
//  Copyright © 2016年 赖虹宇. All rights reserved.
//

import UIKit

class HSMessageViewModel{
    //单一消息模型
    var message : HSMessageModel{
        //在点击回复或者点赞的时候重新计算高度和富文本转换
        didSet{
            contentAttribute = NSAttributedString(string: message.not_content ?? "")
            //计算高度
            setUpBgViewH()
            //计算cell高度
            computeCellH()
        }
    }
    //点赞人名的富文本
    var likeNameAttribute : NSAttributedString = NSAttributedString()
    //回复内容的富文本数组
    var replayAttributeArray : [NSAttributedString] = []
    //给外界使用的回复内容视图高度
    var replayViewH : CGFloat = 0
    //计算整个cell的高度
    var cellHeight : CGFloat = 0
    //详细文本的富文本
    var contentAttribute : NSAttributedString = NSAttributedString()
    
    //因为当前类型没有继承任何父类 ，所以不存在重写父类方法,因此不需要override
    init(model : HSMessageModel) {
        message = model
        //这里处理属性计算和高度计算
        likeNameAttribute = likeAttributeWith(str: message.likelist ?? "")
        contentAttribute = NSAttributedString(string: message.not_content ?? "")
        //计算高度
        setUpBgViewH()
        //计算cell高度
        computeCellH()
    }
    //计算cell高度
    func computeCellH(){
        var h : CGFloat = 0
        //设置默认最大范围
        let maxSize = CGSize(width: replayViewW , height: CGFloat(MAXFLOAT))
      //先计算详细文本的高度
        let contentH = contentAttribute.boundingRect(with: maxSize, options: .usesLineFragmentOrigin, context: nil).size.height
        h = (contentH + 150)
        h += replayViewH
        cellHeight = h
        
    }
    
    //计算回复视图高度和设置内容的富文本
    fileprivate func setUpBgViewH(){
        //高度 = 点赞Label的top + 点赞内容高度 + 间距 + 线高 + (间距 + 回复label的高)*数量 + 间距
        
        //设置默认最大范围
        let maxSize = CGSize(width: replayViewW , height: CGFloat(MAXFLOAT))

        //创建一个总高度的变量
        var viewH : CGFloat = 0
       
        //第一个高度--先判断有没有用内容
        let replayNum = message.replaylist?.count ?? 0
        if message.likenum > 0 ||  replayNum > 0 {
            viewH += 12
        }
        if message.likenum > 0 {
        //计算点赞内容的高度+ 间距和线高
        let likeStrW = likeNameAttribute.boundingRect(with: maxSize, options: NSStringDrawingOptions.usesLineFragmentOrigin , context: nil).size.height
        viewH += (likeStrW + 5 + 1)
        }
        //计算回复内容的高度
        if let replayList = message.replaylist {
            //创建一个可变数组 
             var attrArrayM = [NSAttributedString]()
            for model in replayList {
                //获得回复数据模型
                let replayM = model as! HSReplayListModel
                //先将字符串和名称转换为富文本
                let attrName = NSMutableAttributedString(string: (replayM.int_name ?? "") + " :"
                    , attributes: [NSForegroundColorAttributeName:UIColor().colorWithHex(0x00a0e9),NSFontAttributeName:UIFont.systemFont(ofSize: 13)])
                let attrContent = NSAttributedString(string: replayM.int_content ?? "", attributes: [NSForegroundColorAttributeName:UIColor().colorWithHex(0x4c4c4c),NSFontAttributeName:UIFont.systemFont(ofSize: 13)])
                //拼接字符串
                attrName.append(attrContent)
                attrArrayM.append(attrName)
                //计算高度
                let replayStrH = attrName.boundingRect(with: maxSize, options: .usesLineFragmentOrigin, context: nil).size.height
//                (间距5 + 回复label的高)*数量  因为是循环所以不用乘以数量
                viewH += (5 + replayStrH)
        }
        //赋值给外界
            replayAttributeArray = attrArrayM
            //加上最后的间距
            viewH += 5
            replayViewH = viewH
        }
        
    }
    
    //计算属性和高度
    fileprivate  func likeAttributeWith(str : String) ->
        //设置点赞图片
        NSAttributedString{
        if message.likenum == 0 {
            return NSAttributedString()
        }
        let attach = NSTextAttachment()
        attach.image = UIImage(named: "notifi_support_press")
        attach.bounds = CGRect(x: 0, y: -2, width: 13, height: 13)
        let imageStr = NSAttributedString(attachment: attach)
        //将点赞字符串转换为富文本
            let likeStrAttribute = NSAttributedString(string: " :" + str, attributes: [NSFontAttributeName:UIFont.systemFont(ofSize: 13)])
        //拼接---只能用OC的方式
            let attributeMuble = NSMutableAttributedString(attributedString: imageStr)
            attributeMuble.append(likeStrAttribute)
            
        return attributeMuble
    }
    
}
